package ch2linkedlist;

import static ch2linkedlist.Chapter.*;

public class Ex4 {

	static ListNode addLists(ListNode l1, ListNode l2, int carry) {
		if (l1 == null && l2 == null) {
			return null;
		}
		int d1 = l1 == null ? 0 : l1.val;
		int d2 = l2 == null ? 0 : l2.val;
		ListNode l3 = new ListNode((d1 + d2 + carry) % 10);
		l3.next = addLists(l1 == null ? null : l1.next, 
				l2 == null ? null : l2.next, 
				d1+d2+carry>=10 ? 1 : 0);
		return l3;
	}

	public static void main(String... argv) {
		ListNode num1 = createSampleList(new int[] { 3, 1, 5, 3 });
		ListNode num2 = createSampleList(new int[] { 5, 9, 2 });

		ListNode result = addLists(num1, num2, 0);
		
		print(result);
	}
}
